﻿package com.petermacko.utils {
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.system.System;
	
	public class KeyHandler
	{
		private var stage:Stage;
		private var keysDown:Array = [];
		
		public function KeyHandler(stage:Stage) {
			this.stage = stage;
			setEnabled(true);
		}
		public function setEnabled(b:Boolean):void {
			if(b) {
				stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
				stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			} else {
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
				stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			}
		}
		private function onKeyDown(e:KeyboardEvent):void {
			keysDown[e.keyCode] = true;
		}
		private function onKeyUp(e:KeyboardEvent):void {
			keysDown[e.keyCode] = null;
		}
		public function isDown(code:uint):Boolean {
			return (keysDown[code] != null);
		}
	}
}